/*
 *
 *  MongoServiceMediator.java
 *
 *  Created by CodeRobot on 2014-10-28
 *  Copyright (c) 2014年 com.saike.chexiang All rights reserved.
 *
 */

package com.techwells.heartfeltgift.networkservice;

import android.content.Context;

import com.google.gson.JsonObject;
import com.techwells.heartfeltgift.networkaccess.GiftRequestUrl;
import com.techwells.taco.networkaccessor.NetworkAccess;
import com.techwells.taco.networkaccessor.NetworkResponse;
import com.techwells.taco.networkmodel.BaseUser;
import com.techwells.taco.networkservice.Field_Method_Parameter_Annotation;
import com.techwells.taco.networkservice.ServiceMediator;
import com.techwells.taco.networkservice.ServiceResponse;
import com.techwells.taco.networkservice.ServiceUtils;
import com.techwells.taco.parser.JsonHandler;
import com.techwells.taco.utils.CommonUtil;


public class GiftServiceMediator extends ServiceMediator {

	
	private static GiftServiceMediator mediator;
	
	
	public final static String SERVICE_DO_LOGIN = "doLogin";

	private GiftServiceMediator() {
		super();
	}

	public static GiftServiceMediator sharedInstance() {
		if (mediator == null) {
			synchronized (GiftServiceMediator.class) {
				if (mediator == null) {
					mediator = new GiftServiceMediator();
				}
			}
		}
		return mediator;
	}

	public void setHttpHeader(Context context) {
	
	}
	
	
	/**
	 *  OpenAPI 登陆接口
	 *
	 *  @param account       账号
	 *  @param password      密码
	 *  
	 *  @return
	 */
	@Field_Method_Parameter_Annotation(args = {"username","password"})
	public ServiceResponse<BaseUser> doLogin(String username,String password) {
		// 默认初始化为失败信息
		ServiceResponse<BaseUser> response = new ServiceResponse<BaseUser>();
		response.setOperatCode(GiftRequestUrl.DO_LOGIN);

		JsonObject jsonObj = new JsonObject();
		jsonObj.addProperty("username", username);
		jsonObj.addProperty("password", password);
		//jsonObj.addProperty("clientId", httpHeader.clientId);
		String requestStr = jsonObj.toString();
		// 参数转换为Json
		NetworkResponse netResponse = NetworkAccess.httpRequestAuth(GiftRequestUrl.DO_LOGIN, requestStr);
		
		// HTTP结果返回
		// 1.错误码转义
		String responseStr = ServiceUtils.getRequestResult(response, netResponse);
		if (response.getReturnCode() == ServiceMediator.Service_Return_Success) {
			// 返回成功 解析结果
			JsonHandler.jsonToObject(responseStr, BaseUser.class, response);
			httpHeader.userToken = response.getResponse().token;
		}
		return response;
	}
	
	
}
